package com.jwdemo
{
	import com.jwdemo.TextureAnim;
	
	import flash.display.BitmapData;
	
	// Simple SpriteSheet - all sprites have the same pixel width/height.
	// TODO 'SpriteBag' class - which will allow user defined dimensions for individual sprites

	public class SpriteSheet
	{
		static private const DEFAULT_FPS:Number	=	12 ;

		private var image:BitmapData ;
		private var pixelWidth:uint ;
		private var pixelHeight:uint ;
		private var cellsAcross:uint = 0 ;
		private var cellsDown:uint = 0 ;
		private var width:uint ;
		private var height:uint ;
		private var numFrames:uint ;
		private var offsets:Array  = [];
		private var uvVectorData:Array ; //<Vector.<Number>> ;
		
		public function SpriteSheet(image:BitmapData, width:uint, height:uint)
		{
			this.image			=	image ;
			this.pixelHeight	=	height ;
			this.pixelWidth		=	width ;
			this.width			=	image.width ;
			this.height			=	image.height ;	
			cellsAcross 		=	image.width / pixelWidth ;
			cellsDown 			=	image.height / pixelHeight ;
			numFrames			=	cellsAcross * cellsDown ;
			uvVectorData 		= 	new Array() ;

			
			var vres:int 				=	this.width/(pixelWidth) ;
			var hres:int 				=	this.height/(pixelHeight) ;

			var px:Number 				=	0 ;	// move this up and out!
			var py:Number 				=	0 ;

			for (var frm:int = 0 ; frm < numFrames ; frm++)
			{
				var x:int = _getFrameXOffset(frm) ;
				var y:int = _getFrameYOffset(frm) ;
				offsets.push({x: x, y: y}) ;
				
				
				// Build up uvData for drawTriangles call - is this really faster for
				// just the one section?
				var uvData:Vector.<Number> 	= 	new Vector.<Number>() ;
				var j:int					=	int(frm / cellsAcross) ;
				var i:int					=	(frm % cellsAcross) ;		
				
				uvData.push((i+px)/vres,(j+py)/hres,(i+1-px)/vres,(j+py)/hres,(i+1-px)/vres,(j+1-py)/hres,(i+px)/vres,(j+1-py)/hres);
			
				uvVectorData.push(uvData) ;

			}
			

		}
		public function getMidX(frame:uint):Number
		{
			return _getFrameXOffset(frame) + pixelWidth*0.5 ;
		}
		
		public function getMidY(frame:uint):Number
		{
			return _getFrameYOffset(frame) + pixelHeight*0.5 ;
					
		}

		public function getUVVectorData(frame:uint):Vector.<Number> 
		{
			return this.uvVectorData[frame % numFrames];	
		}
		
		public function getNumberOfHorizontalFrames():uint
		{
			return this.cellsAcross ;
		}
		
		public function getFrameWidth():uint
		{
			return this.pixelWidth ;
		}
		
		public function getFrameHeight():uint
		{
			return this.pixelHeight ;
			
		}

		public function getSheetWidth():uint
		{
			return this.width ;
		}
		
		public function getSheetHeight():uint
		{
			return this.height ;
			
		}

		public function getPixelWidth() : uint 
		{
			
			return this.pixelWidth ;
		}
		
		public function getPixelHeight() : uint 
		{
			return this.pixelHeight ;
		}

		
		public function _getFrameXOffset(frame:uint) : uint
		{
			return (frame % cellsAcross)*pixelWidth  ;
		}
		
		public function _getFrameYOffset(frame:uint) : uint
		{
			return (int(frame / cellsAcross))*pixelHeight  ;
		}

		public function getNumberOfFrames(): uint
		{
			return numFrames ;
		}
		
		public function getImage() : BitmapData 
		{
			return this.image ;
		}
		
		
		public function getFrameXOffset(frame:uint) : int
		{
			return offsets[frame].x ;
		}
			
		public function getFrameYOffset(frame:uint) : int
		{
			return offsets[frame].y ;

		}

		public function createTextureAnimation(fps:Number = 12,timeout:Number = 0.0) : TextureAnim
		{
			var animation:TextureAnim	=	new TextureAnim(fps,timeout) ;
			
			for (var frame:uint = 0 ; frame < numFrames ; frame++)
			{
				animation.addFrame({frame:frame, sheet:this, uvData:getUVVectorData(frame), image: image}) ;	
			}
			return animation ;
		}
		
		
	}
}